package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Resource
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * 1、因为前端没有传这个id给我们,但（数据库这个字段不能为null）,获取当前登录用户的id，设置购物车表中的user_id为当前用户id,指定当前是哪个用户的购物车数据
     * 2、查询该用户的购物车数据，判断添加到购物车的是菜品/套餐
     * 看当前添加的菜品或者套餐是否在购物车中
     *      如果存在，就数量+1
     *      如果不存在，添加购物车，数量默认1
     * @param shoppingCart
     * @return
     */

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart)
    {

        //获取当前登录用户的id，设置购物车表中的user_id为当前用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //查询该用户的购物车数据，先判断用户添加到购物车的是菜品还是套餐，再看当前添加的菜品或者套餐是否已经在购物车中
        //SQL:select * from shopping_cart where user_id = ? and dish_id/setmeal_id = ?
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        //先判断用户添加到购物车的是菜品还是套餐，如果菜品id不为空
        if (shoppingCart.getDishId() != null)
        {
            //则添加到购物车的是菜品
            wrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());

        }
        else
        {
            //否则添加到购物车的是套餐
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //查询当前添加的菜品或者套餐是否在购物车中
        //SQL:select * from shopping_cart where user_id = ? and dish_id/setmeal_id = ?
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        //one不为空则在购物车中
        if (one != null)
        {
            //如果存在，菜品或者套餐数量+1
            one.setNumber(one.getNumber() + 1 );
            //保存更新
            shoppingCartService.updateById(one);
        }
        else
        {
            //如果不存在，添加到购物车，插入操作，数量默认1
            shoppingCart.setNumber(1);
            //入库单时候为创建时间的字段进行赋值
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            //将保存的数据赋值给查询对象one，方便响应结果查看
            one = shoppingCart;
        }
        return R.success(one);
    }

    /**
     * 减少购物车数据
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){

        //获取当前登录用户的id，设置购物车表中的user_id为当前用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //查询该用户的购物车数据，先判断用户添加到购物车的是菜品还是套餐，再看当前添加的菜品或者套餐是否已经在购物车中
        //SQL:select * from shopping_cart where user_id = ? and dish_id/setmeal_id = ?
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
        //先判断用户添加到购物车的是菜品还是套餐，如果菜品id不为空
        if (shoppingCart.getDishId() != null)
        {
            //则添加到购物车的是菜品
            wrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());

        }
        else
        {
            //否则添加到购物车的是套餐
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //查询当前添加的菜品或者套餐是否在购物车中
        //SQL:select * from shopping_cart where user_id = ? and dish_id/setmeal_id = ?
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        //先减1后判断，不然数据表中数量字段会变成0而不是直接清除数据表中数据
        one.setNumber(one.getNumber() - 1);
        if (one.getNumber() > 0 )
        {
            //如果查询该菜品或套餐在购物车中，判断数量，数量>0则数量-1，否则直接清除该菜品或者套餐数据

            shoppingCartService.updateById(one);
        }
        else
        {
            shoppingCartService.removeById(one);
        }

        return R.success(one);
    }



    /**
     * 清空购物车
     * 根据用户id查询购物车所有数据并删除
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean()
    {
        //SQL:delete from shopping_cart where user_id = ?
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(wrapper);
        return R.success("清空购物车成功");
    }

    /**
     * 查看添加到购物车的数据
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list()
    {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        wrapper.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list);
    }
}
